package com.example.panpan.orderManage.controller;

import com.example.common.utils.IMap;
import com.example.common.utils.PageView;
import com.example.common.utils.ReturnMapUtils;
import com.example.common.utils.SessionUtil;
import com.example.panpan.orderManage.service.OrderService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Controller
public class orderController {
    @Autowired
    private OrderService orderService;

    /**
     * 跳转订单管理页面
     * */
    @RequestMapping(value = "/orderManage/toPage/orderManageView")
    public String orderManageView(){
        return "/module/orderManage/orderManage";
    }
    /**
     * 跳转产品出售页面
     * */
    @RequestMapping(value = "/orderManage/toPage/orderAddView")
    public String orderAddView(){
        return "/module/orderManage/addOrder";
    }
    /**
     * 弹出选择产品页面
     * */
    @RequestMapping(value = "/orderManage/toPage/chooseProductView")
    public String chooseProductView(HttpServletRequest request,ModelMap modelMap){
        IMap param = new IMap(request);
        modelMap.addAttribute("customerId",param.getString("customerId"));
        return "/module/orderManage/chooseProduct";
    }
    /**
     * 弹出订单修改页面
     * */
    @RequestMapping(value = "/orderManage/toPage/orderUpdView")
    public String orderUpdView(HttpServletRequest request,ModelMap modelMap){
        IMap params = new IMap(request);
        IMap thisOrder = orderService.getOrderInfo(params);
        modelMap.addAttribute("thisOrder",thisOrder);
        return "/module/orderManage/updOrder";
    }
    /**
     * 获得订单列表
     * */
    @RequestMapping(value = "/orderManage/ajax/getOrderList",method = RequestMethod.POST)
    @ResponseBody
    public Map getOrderList(HttpServletRequest request){
        IMap params = new IMap(request);
        Page page = PageView.startPageForTable(params, false);
        List<IMap> orderrList = orderService.getOrderList(params);
        Map<String, Object> result = PageView.getResultForTable(page, orderrList);
        return result;
    }
    /**
     * 修改订单信息
     */
    @RequestMapping(value = "/orderManage/ajax/updOrder",method = RequestMethod.POST)
    @ResponseBody
    public Map updOrder(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = orderService.updOrder(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","修改成功！");
        }else if (i < 0){
            return ReturnMapUtils.businessResult("9999","服务器错误，修改失败！");
        }else {
            return ReturnMapUtils.businessResult("9999","库存不足！剩余可增加库存量："+i);
        }
    }
    /**
     * 修改订单状态
     * */
    @RequestMapping(value = "/orderManage/ajax/updOrderState",method = RequestMethod.POST)
    @ResponseBody
    public Map updOrderState(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = orderService.updOrderState(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","状态修改成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，状态修改失败！");
        }
    }
    /**
     * 创建订单
     * */
    @RequestMapping(value = "/orderManage/ajax/createOrder",method = RequestMethod.POST)
    @ResponseBody
    public Map createOrder(HttpServletRequest request){
        IMap params = new IMap(request);
        HttpSession session = request.getSession();
        String userId = SessionUtil.getLoginUserId(session);
        params.put("updateUser",userId);
        int i = orderService.createOrder(params);
        if (i==1){
            return ReturnMapUtils.businessResult("0000","创建订单成功！");
        }else {
            return ReturnMapUtils.businessResult("9999","服务器错误，创建订单失败！");
        }
    }

    /**
     * 跳转订单报表页面
     * */
    @RequestMapping(value = "/orderManage/toPage/orderReport")
    public String orderReport(){
        return "/module/report/orderReport";
    }
}
